#pragma once
// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: script_controller_impl.h
// Description: ScriptController Class
//      Author: Ziming Li
//     Created: 2022-03-02
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

/*
 * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BLINKIT_SCRIPT_CONTROLLER_IMPL_H
#define BLINKIT_SCRIPT_CONTROLLER_IMPL_H

#include "blinkit/blink/renderer/bindings/core/qjs/prototype_traits.h"
#include "blinkit/blink/renderer/platform/heap/Handle.h"
#include "blinkit/blink/renderer/wtf/Noncopyable.h"
#include "blinkit/blink/renderer/wtf/text/AtomicStringHash.h"
#include "third_party/quickjs/quickjs.h"

class RuntimeImpl;

namespace BlinKit {
class ContextClient;
}

namespace blink {

class Element;
class Event;
class EventListener;
class EventTarget;
class LocalDOMWindow;
class Node;
class ScriptWrappable;

class ScriptController
{
    WTF_MAKE_NONCOPYABLE(ScriptController);
public:
    virtual ~ScriptController(void);

    static ScriptController* From(JSContext *ctx);

    JSContext* EnsureContext(void);
    LocalDOMWindow* GetWindow(void);
    LocalFrame& GetFrame(void) { return m_frame; }

    void SetContextClient(ContextClient *client) { m_client = client; }

    template <class T>
    JSValue Wrap(T *p) {
        return WrapImpl(p, qjs::PrototypeTrait<T>::PROTO);
    }

    JSValue WrapEventTarget(EventTarget *eventTarget);
    JSValue WrapNode(Node *node);
    JSValue WrapElement(Element *element);
    JSValue WrapEvent(Event *event);

    /**
     * Event Listener
     */
    EventListener* FindEventListener(JSValue scriptObject) const;
    void RegisterEventListener(JSValue scriptObject, EventListener *listener);
    void UnregisterEventListener(JSValue scriptObject);

    void ExecutePendingJobs(void);

    /**
     * Common Exports
     */
    virtual bool canExecuteScripts(ReasonForCallingCanExecuteScripts) { return true; }
    void clearForClose(void);
    void clearWindowProxy(void);
    void executeScriptInMainWorld(const ScriptSourceCode &sourceCode);
    constexpr bool shouldBypassMainWorldCSP(void) const { return true; }
    void updateDocument(void);
protected:
    ScriptController(LocalFrame &frame);

    using PrototypeMap = std::unordered_map<WTF::AtomicString, JSValue>;
    struct Prototypes {
        JSValue prototypes[qjs::PROTO_MAX];
        PrototypeMap elementPrototypes;
        PrototypeMap eventPrototypes;

        Prototypes(void);
        void Cleanup(JSContext *ctx);

        JSValue FindElementPrototype(const AtomicString &tagName) const;
        JSValue FindEventPrototype(const AtomicString &interfaceName) const;
    private:
        static void CleanupPrototypeMap(JSContext *ctx, PrototypeMap &protoMap);
    };

    static void RegisterCharacterDataPrototypes(JSContext *ctx, Prototypes &dst, JSValue protoNode);

    LocalFrame &m_frame;
private:
    static void RegisterCommonPrototypes(JSContext *ctx, Prototypes &dst);
    virtual void RegisterPrototypes(JSContext *ctx, Prototypes &dst) = 0;
    static void PrepareContext(JSContext *ctx, const Prototypes &prototypes);
    void Clear(void);

    JSValue WrapImpl(ScriptWrappable *nativeObject, int prototype);
    JSValue NewScriptWrappable(ScriptWrappable *nativeObject, JSValue proto);
    JSValue NewNode(Node *node);
    JSValue NewElement(Element *element);

    ContextClient *m_client = nullptr;
    std::unique_ptr<RuntimeImpl> m_rt;
    JSContext *m_ctx = nullptr;
    Prototypes m_prototypes;
    std::unordered_map<void *, EventListener *> m_eventListeners;
};

} // namespace blink

#endif // BLINKIT_SCRIPT_CONTROLLER_IMPL_H
